<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 6/10/14
 * Time: 2:41 PM
,,*/

class Site_Autocomplete_Controller extends Base_Controller {

    public $restful = true;

    public function __construct()
    {
        parent::__construct();
    }

    public function get_category(){

        $term = $_GET["q"];
        $cid = Categories::select(array('id','name'))->where('name', 'LIKE', $term.'%')->take(7)->get();

        if($cid) {
            foreach ($cid as $row){
                $answer[] = array("id"=>$row->id,"text"=>$row->name);
            }
        } else {
            // 0 results send a message back to say so.
            $answer[] = array("id"=>"0","text"=>"No Results Found..");
        }


        echo json_encode($answer);//format the array into json data
    }

    public function get_keyword($id){

        $term = $_GET["q"];
        $cid = Categories::select(array('id','name'))
            ->where('parent_id', '=', $id)
            ->where('name', 'LIKE', $term.'%')
            ->take(7)->get();

        if($cid) {
            foreach ($cid as $row){
                $answer[] = array("id"=>$row->id,"text"=>$row->name);
            }
        } else {
            // 0 results send a message back to say so.
            $answer[] = array("id"=>"0","text"=>"No Results Found..");
        }


        echo json_encode($answer);//format the array into json data
    }

    public function get_location(){

        $term = $_GET["q"];
        $cid = Locations::select(array('id','name','prefix'))->where('name', 'LIKE', $term.'%')->take(7)->get();
        if($cid) {
            foreach ($cid as $row){
                $nn = ($row->prefix ? $row->name.', '.$row->prefix : $row->name);
                $answer[] = array("id"=>$row->id,"text"=>$nn);
            }
        } else {
            // 0 results send a message back to say so.
            $answer[] = array("id"=>"0","text"=>"No Results Found..");
        }

        echo json_encode($answer);//format the array into json data
    }

    public function get_business(){

        $term = $_GET["q"];
        $cid = Post::select(array('id','title'))->where('title', 'LIKE', $term.'%')->take(7)->get();
        if($cid) {
            foreach ($cid as $row){
                $answer[] = array("id"=>$row->id,"text"=>$row->title);
            }
        } else {
            // 0 results send a message back to say so.
            $answer[] = array("id"=>"0","text"=>"No Results Found..");
        }
        echo json_encode($answer);//format the array into json data
    }

    public function get_child_category($cid){

        $deep = Hierarchy::max_deep('categories',$cid);
        $cat = Categories::where('parent_id', '=', $cid)
            ->where('deep', '<', $deep)
            ->get();
        $num_rows = count($cat);

        if($num_rows > 0)
        {

            $tt = "<select name='category' class='parent field' id='child-category'>";
            $tt .=  "<option value=''>--Select--</option>";
            foreach($cat as $cat){
                $tt .=  "<option value='$cat->id'>$cat->name</option>";
            }
            $tt .= "</select>";

            echo $tt;
            exit();
        }

        $cat = Categories::where('parent_id', '=', $cid)
            ->where('deep', '=', $deep)
            ->get();
        $num_rows = count($cat);
        if($num_rows > 0)
        {

            $tt = "<label> Keyword <span class='text-danger thin'>(Select Multiple)</span> </label>";
            $tt .= "<ul name='keywords'class='multiple' id='child-category'>";
            foreach($cat as $cat){
                $tt .=  "<li><input type='checkbox' name='keywords[]' id='keywords' value='$cat->id' > &nbsp; $cat->name</li>";
            }
            $tt .= "</ul>";
            echo $tt;
            exit();
        }


    }

    public function get_child_key($cid){

        $deep = Hierarchy::max_deep('categories',$cid);
        $cat = Categories::where('parent_id', '=', $cid)
            ->where('deep', '=', $deep)
            ->get();
        $num_rows = count($cat);

        if($num_rows > 0)
        {
            $tt = "<label> Keyword <span class='text-danger thin'>(Select Multiple)</span> </label>";
            $tt .= "<ul class='multiple' id='show_keyword'>";
            foreach($cat as $cat){
                $tt .=  "<li><input type='checkbox' name='keywords[]' id='keywords' value='$cat->id' > &nbsp; $cat->name</li>";
            }
            $tt .= "</ul>";
            echo $tt;
            exit();
        }
    }

} 